package com.southminority.ethnic.controller.alioss;

import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.alioss.vo.*;
import com.southminority.ethnic.service.alioss.OssChunkUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "阿里OSS分片上传", description = "OSS分片上传接口")
@RestController
@RequestMapping("/aliyunOss/chunk")
@CrossOrigin
public class OssChunkController {
    
    @Autowired
    private OssChunkUploadService ossChunkUploadService;
    
    @Operation(summary = "初始化分片上传")
    @PostMapping("/init")
    public R<OssChunkInitRespVo> initChunkUpload(@Validated @RequestBody OssChunkInitReqVo req) {
        try {
            log.info("初始化分片上传: fileMd5={}, fileName={}", req.getFileMd5(), req.getFileName());
            return R.ok(ossChunkUploadService.initChunkUpload(req));
        } catch (Exception e) {
            log.error("初始化分片上传失败", e);
            return R.fail(e.getMessage());
        }
    }
    
    @Operation(summary = "上传分片")
    @PostMapping("/upload")
    public R<OssChunkUploadRespVo> uploadChunk(@Validated OssChunkUploadReqVo req) {
        try {
            log.info("上传分片: uploadId={}, chunkIndex={}", req.getUploadId(), req.getChunkIndex());
            return R.ok(ossChunkUploadService.uploadChunk(req));
        } catch (Exception e) {
            log.error("上传分片失败", e);
            return R.fail(e.getMessage());
        }
    }
    
    @Operation(summary = "合并分片")
    @PostMapping("/merge")
    public R<String> mergeChunks(@Validated @RequestBody OssChunkMergeReqVo req) {
        try {
            log.info("合并分片: uploadId={}, totalChunks={}", req.getUploadId(), req.getTotalChunks());
            String fileUrl = ossChunkUploadService.mergeChunks(req);
            return R.ok(fileUrl);
        } catch (Exception e) {
            log.error("合并分片失败", e);
            return R.fail(e.getMessage());
        }
    }
    
    @Operation(summary = "取消分片上传")
    @PostMapping("/cancel/{uploadId}")
    public R<String> cancelChunkUpload(@PathVariable String uploadId) {
        try {
            log.info("取消分片上传: uploadId={}", uploadId);
            ossChunkUploadService.cancelChunkUpload(uploadId);
            return R.ok("取消成功");
        } catch (Exception e) {
            log.error("取消分片上传失败", e);
            return R.fail(e.getMessage());
        }
    }
    
    @Operation(summary = "检查文件是否存在")
    @GetMapping("/check/{fileMd5}")
    public R<String> checkFileExists(@PathVariable String fileMd5) {
        try {
            String fileUrl = ossChunkUploadService.checkFileExists(fileMd5);
            if (fileUrl != null) {
                return R.ok(fileUrl);
            }
            return R.fail("文件不存在");
        } catch (Exception e) {
            log.error("检查文件失败", e);
            return R.fail(e.getMessage());
        }
    }
}
